const DB = wx.cloud.database().collection("aid_news")
const DB_swiper = wx.cloud.database().collection("aid_swiper")
// 轮播图设置
const app = getApp()

Page({
  data: {
    news:[],
    rollData:[],
    text0: '实时新闻热点',
    
    imgUrls: [
      'https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_news/swiper/aid_news_1.jpg?sign=38fa4eb7e4a8c00735ad10111b6a0ac9&t=1584172958',
      'https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_news/swiper/aid_news_2.jpg?sign=3edc2712fd332ae1469972d24d9b36aa&t=1584173112',
      'https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_news/swiper/aid_news_3.jpg?sign=d42f3c6ade625001b38ef5f998cb7120&t=1584173131'
    ],
    //是否显示画板指示点 
    circular: true,
    //选中点的颜色 
    indicatorDots: true,
    //是否竖直 
    vertical: false,
    //是否自动切换 
    autoplay: true,
    //自动切换的间隔
    interval: 3000,
    //滑动动画时长毫秒 
    duration: 1000,
    //所有图片的高度 
    imgheights: [],
    //图片宽度 
    imgwidth: 750,
    //默认 
    current: 0,
  },
  imageLoad: function (e) {//获取图片真实宽度 
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比 
      ratio = imgwidth / imgheight;
    console.log(imgwidth, imgheight)
    //计算的高度值 
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    var imgheights = this.data.imgheights;
    //把每一张图片的对应的高度记录到数组里 
    imgheights[e.target.dataset.id] = imgheight;
    this.setData({
      imgheights: imgheights
    })
  },
  bindchange: function (e) {
    this.setData({ current: e.detail.current })
  },
  // 跳转至详情页
  toNewsDetail(event) {
    console.log("点击获取的数据", event.currentTarget.dataset.item._id)
    wx.navigateTo({
      url: '../../pages/aid_news_detail/aid_news_detail?id=' + event.currentTarget.dataset.item._id,
    })
  },

  onLoad: function (options) {
    console.log(options);

    var that = this;
    //滚动播报连接数据库aid_swiper
    DB_swiper.get({    
      success: res => {
        console.log(res.data)   
        this.setData({
          rollData: res.data
        })
      }
    })

    // //1、引用数据库   
    // const db = wx.cloud.database({
    //   //这个是环境ID不是环境名称     
    //   env: 'main-wc2xz'
    // })
    // //2、开始查询数据了  news对应的是集合的名称   
    // db.collection('aid_news').get({

    //新闻主体连接aid_news 
    DB.get({
      //如果查询成功的话    
      success: res => {
        console.log(res.data)
        //这一步很重要，给news赋值，没有这一步的话，前台就不会显示值      
        this.setData({
          news: res.data
        })
      }
    })
  },

})
